package com.isdepci.project.carbon.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.isdepci.common.utils.DateUtils;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.ProductCarbonConsumables;
import com.isdepci.project.carbon.mapper.ProductCarbonConsumablesMapper;
import com.isdepci.project.carbon.mapper.ProductCarbonStageMapper;
import com.isdepci.project.carbon.utils.ProductCarbonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.carbon.mapper.ProductCarbonMapper;
import com.isdepci.project.carbon.domain.ProductCarbon;
import com.isdepci.project.carbon.service.IProductCarbonService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品碳足迹Service业务层处理
 *
 * @author isdepci
 * @date 2024-11-07
 */
@Service
public class ProductCarbonServiceImpl implements IProductCarbonService {
    @Autowired
    private ProductCarbonMapper productCarbonMapper;
    @Autowired
    private ProductCarbonUtils productCarbonUtils;
    @Autowired
    private ProductCarbonStageMapper productCarbonStageMapper;
    @Autowired
    private ProductCarbonConsumablesMapper productCarbonConsumablesMapper;

    @Override
    public int calCarbonQuantityById(Long id) {
        List<ProductCarbonConsumables> list = productCarbonConsumablesMapper.selectConsumablesListByProductCarbonId(id);
        if(list==null||list.size()<=0){
           return productCarbonMapper.updateProductCarbon(new ProductCarbon(id, BigDecimal.ZERO,DateUtils.getNowDate()));
        }
        BigDecimal sum = BigDecimal.ZERO;
        for (ProductCarbonConsumables item : list) {
            if(item.getFactorNum()!=null&&item.getQuantity()!=null){
                sum = sum.add(item.getFactorNum().multiply(item.getQuantity()));
            }
        }
        return productCarbonMapper.updateProductCarbon(new ProductCarbon(id, sum,DateUtils.getNowDate()));
    }

    /**
     * 查询产品碳足迹
     */
    @Override
    public ProductCarbon selectProductCarbonById(Long id) {
        ProductCarbon info= productCarbonMapper.selectProductCarbonById(id);
        if(info!=null){
            info.setStageList(productCarbonStageMapper.selectStageListByProductCarbonId(id));
        }
        return info;
    }
    @Override
    public ProductCarbon getInfoAndConsumables(Long id) {
        ProductCarbon info= productCarbonMapper.selectProductCarbonById(id);
        if(info!=null){
            info.setConsumables(productCarbonConsumablesMapper.selectConsumablesListByProductCarbonId(id));
            info.setStageList(productCarbonStageMapper.selectStageListByProductCarbonId(id));
        }
        return info;
    }

    /**
     * 查询产品碳足迹列表
     */
    @Override
    public List<ProductCarbon> selectProductCarbonList(ProductCarbon productCarbon) {
        return productCarbonMapper.selectProductCarbonList(productCarbon);
    }

    /**
     * 新增产品碳足迹
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertProductCarbon(ProductCarbon productCarbon) {
        if(productCarbon.getProductId()==null){
           return AjaxResult.error("未获取到相关产品信息");
        }
        productCarbon.setCreateUser();
        int row = productCarbonMapper.insertProductCarbon(productCarbon);
        if(row>0){
            productCarbonUtils.genProductCarbonDataByInsert(productCarbon);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改产品碳足迹
     */
    @Override
    public int updateProductCarbon(ProductCarbon productCarbon) {
        productCarbon.setUpdateUser();
        return productCarbonMapper.updateProductCarbon(productCarbon);
    }




}
